<?php
/**
 * Created by PhpStorm.
 * User: HXL
 * Date: 2017/5/8
 * Time: 15:36
 */
namespace app\orders\controller;

use app\common\ErrorCodes;
use think\Lang;

class Admin extends Base{
    /**
     *查询修改的信息
     */
    public function one(){
        $uid = input('param.uid');
        $where = array('uid'=>$uid);
        $field = 'uid,username,surname,password,phone,status_u,create_time_u,role_id,de_id,sale_id';
        $data = $this->admins()->select($field, $where);
        return $this->show($data);
    }

    /**
     * 用户列表
     * */
    public function index(){
//        $this->authVerify();
        $field = 'uid,username,surname,password,phone,status_u,create_time_u,role_id,de_id,sale_id';
        //显示比自己权限小的用户（0管理员，1用户，2表计组）
        $status = session('order_status');
        $where = array('status_u'=>['>=', $status]);
        //$where = '';
        $order = 'create_time_u asc';
        $admin = $this->admins()->joinPage($where, $order);
        $admin = $this->resetAdmins($admin);
        return $this->show($admin);
    }

    /**
     * 增加用户
     * */
    public function save(){
//        $this->authVerify();
        $hex = $this->hex();
        $ad = $this->admins();
        $admin = input('post.');
        //$postPassword = input('param.password');    //获取的默认密码值：123456；
        $admin['password'] = '123456';
        $admin['password'] = $hex->encrypt($admin['password']);   //密码用AES加密；
        $time = time();
        $admin['create_time_u'] = date('Y-m-d H:i:s', $time);
        //var_dump($admin);exit();
        $where = array();
        //判断
        $username = $admin['username'];
        $find = $ad->findById(array('username'=>$username));
        if($find){
            return $this->show(array(), ErrorCodes::$USER_EXIST, Lang::get('existed user'));
//            return $this->error(Lang::get('existed user'));
        }
        $add = $ad->add($admin, $where);
        if (!$add){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('add fail'));
//            return $this->error(Lang::get('add fail'));
        }
        return $this->show($admin, ErrorCodes::$SUCCESS, Lang::get('add success'));
//        return $this->success(Lang::get('add success'), 'Admin/index');
    }

    /**
     * 用户信息编辑
     * */
    public function esave(){
        $this->authVerify();
//        $time = $_SERVER['REQUEST_TIME'];         //客户端向服务端发送请求的时间
        $admin = input('post.');
//        $admin['password'] = $this->hex()->encrypt($admin['password']);   //密码用AES加密；
        $where = array('uid'=>$admin['uid']);
        $findUser = $this->admins()->findById($where);
        //var_dump($findUser);exit();
        if (!$findUser){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind user'));
//            return $this->error(Lang::get('unfind user'));
        }
        //var_dump($admin);exit();
        $result = $this->admins()->update($admin, $where);
        if (!$result){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('edit fail'));
//            return $this->error(Lang::get('edit fail'));
        }
        return $this->show($admin, ErrorCodes::$SUCCESS, Lang::get('edit success'));
//        return $this->success(Lang::get('edit success'), 'Admin/index');
    }
    /**
     * 删除用户
     * */
    public function del(){
        $this->authVerify();
        $uid = input('param.uid');
        //var_dump($uid);exit();
        $admin = $this->admins();
        $where = array('uid'=>$uid);
        $user = $this->admins()->findById($where);
        $self = session('order_user');
        //var_dump($self);exit();
        if($self == $user['username']){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('del self unallowed'));
//            return $this->error(Lang::get('del self unallowed'));
        }
        if($user['status_u'] == 0){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('del root unallowed'));
//            return $this->error(Lang::get('del root unallowed'));
        }
        $delete = $admin->del($where);
        if (!$delete){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('del fail'));
//            return $this->error(Lang::get('del fail'));
        }
        return $this->show($user, ErrorCodes::$SUCCESS, Lang::get('del success'));
//        return $this->success(Lang::get('del success'), 'Admin/index');
    }
    /**
     * @return mixed
     * 模糊查询
     */
    public function search(){
        $search = input('param.search');
        $data = $this->admins()->searchLike($search);
        $data = $this->resetAdmins($data);
        return $this->show($data);
        /*$this->page($data);
        $this->asgDepts();
        $this->asgRoles();
        $this->assign('admin', $data);
        return $this->fetch('user/index');*/

    }

    /**
     * 用户信息重新组合
     * @param $admins
     * @return mixed
     */
    private function resetAdmins($admins){
        $len = count($admins);
        for ($i=0;$i<$len;$i++){
            $admins[$i] = $this->resetAdmin($admins[$i]);
        }
        return $admins;
    }

    /**
     * 单个用户信息重新组合
     * @param $admin
     */
    private function resetAdmin($admin){
        $len = count($admin);
        if($len >= 1){
            $sale = $this->setSaleDepts($admin['sale_id']);
            $admin['sale'] = $sale;
            //用户类型
            $admin['is_status'] = $this->zhChange($admin['status_u']);
        }else{
            $admin['sale'] = '';
            $admin['is_status'] = '';
        }
        return $admin;

    }

    /**
     * 用户类型中文转换
     * @param $status
     */
    private function zhChange($status){
        switch ($status){
            case 0:
                $is_status = '超级管理员';
                break;
            case 1:
                $is_status = '管理员';
                break;
            case 2:
                $is_status = '用户';
                break;
            case 3:
                $is_status = '表计组';
                break;
            default:
                $is_status = '其他';
                break;
        }
        return $is_status;
    }



}